// pages/mine/pages/my/integral/integral.js

const util = require('../../../../../utils/util');
const request = require('../../../../../utils/request')
const WxParse = require('../../../../../wxParse/wxParse/wxParse.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarHeight: 0,
    scrollTop: 0,
    bankList: [],
    selectBankObj: {},
    isShowCart: false,
    activeIndex: 0,
    balance: 0,
    money: '',
    bankId:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      navbarHeight: util.getNavbarHeight(),
      userInfo: wx.getStorageSync('userInfo')
    })
    //改变头部电量条背景色
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ffffff',
    })
   
  },
  getBankList() {
    request._post('/api?method=user.getbankcardlist', {

    }, 'noload').then(res => {
      if (res.data.length > 0) {
        this.setData({
          bankList: res.data
        })
        this.data.bankList.forEach(function (obj, index) {
          obj.index = index
        })
        this.getDefaultBank()
      }

    })
  },
  selectYh() {
    this.setData({
      isShowCart: true
    })
  },
  hideWindow() {
    this.setData({
      isShowCart: false
    })
  },

  // 获取默认结算卡
  getDefaultBank() {
    request._post('/api?method=user.getdefaultbankcard', {}, 'noload').then(res => {
      console.log(res)
      let activeIndex = this.data.bankList.find(el => el.id == res.data.id).index

      this.setData({
        selectBankObj: res.data,
        activeIndex
      })
    })
  },
  chooseBank(e) {
    console.log(e, 'jn')
    let obj = e.currentTarget.dataset
    this.setData({
      activeIndex: obj.index,
      selectBankObj:obj
    })
  },
  confirmOrder() {
    this.setData({
      selectBankObj: this.data.bankList[this.data.activeIndex],
      isShowCart: false,
    })
  },
  // 提现前置操作
  cashBefore() {
    request._post('/api?method=user.cashBefore', {}, 'noload').then(res => {
      console.log(res)
      this.setData({
        settingObj: res.data.setting,
        balance: res.data.balance,

      })
      WxParse.wxParse('detail_content', 'html', res.data.setting.tocash_money_rule, this, 5);
    })
  },
  allTx() {
    this.setData({
      money: this.data.balance
    })
  },
  confirmTx() {
    if (!this.data.balance) {
      wx.showToast({
        title: '余额为0,无法提现',
        icon: 'none'
      })
      return
    }

    if (!this.data.money) {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return
    }
    if(this.data.money > this.data.balance){
      wx.showToast({
        title: '提现金额大于余额，请重新输入',
        icon: 'none'
      })
      return
    }

    if (this.data.money < this.data.settingObj.tocash_money_low) {
      wx.showToast({
        title: '最低提现额度为' + this.data.settingObj.tocash_money_low,
        icon: 'none'
      })
      return
    }
    if(!this.data.selectBankObj.id){
      wx.showToast({
        title: '请选择提现银行',
        icon: 'none'
      })
      return
    }

    request._post('/api?method=user.cash', {
      money:this.data.money,
      cardId:this.data.selectBankObj.id
    }, 'noload').then(res => {
      wx.showToast({
        title:res.msg,
        icon:'none'
      })

    })
  },
  inputWrite(e) {
    this.setData({
      money: e.detail.value
    })
  },
  addBank() {
    wx.navigateTo({
      url:'/pages/mine/pages/my/bankcard-add/bankcard-add'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBankList()
    this.cashBefore()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  back() {
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})